package com.accenture.adf.newcodington.module14.sample;

//Sample Singleton
public class Singleton {

	
	//This is a class variable. It would be referred to in getInstance()
	private static Singleton onlyInstance;
	
	//Constructor is private so that on other class can call it
	private Singleton(){
		System.out.println("This is Singleton's constructor. I should be called only once");
	}
	
	//A static method to ensure that only one instance of this class is created at any given point in time.
	//It is static so that other classes can call it using its class name
	//it can refer to onlyInstance directly because that is static too 
	public static Singleton getInstance(){
		if(onlyInstance == null){
			onlyInstance = new Singleton(); //Initialize if it's null
		}
		return onlyInstance;//else in all other cases, return the reference to the existing object
	}
	
}
